import createReducer from '../lib/createReducer';
import * as types from '../actions/types';

import { AppNavigator } from '../components/AppNavigator'
import { NavigationActions } from "react-navigation";

export const navReducer = createReducer( AppNavigator.router.getStateForAction(AppNavigator.router.getActionForPathAndParams('Home')) ,{
  [types.SET_ROUTE](state,action){
    const navigateAction = NavigationActions.navigate({ routeName: action.direccion});
    const nextState = AppNavigator.router.getStateForAction(
      navigateAction,
      state);
    // Simply return the original `state` if `nextState` is null or undefined.
    return nextState || state;
  },
  ["Navigation/BACK"](state,action){
    const navigateAction = NavigationActions.back();
    const nextState = AppNavigator.router.getStateForAction(
      navigateAction,
      state);
    // Simply return the original `state` if `nextState` is null or undefined.
    return nextState || state;
  }
});
